package cn.vcjf.weapp.api.user.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.ResponseBody;
import cn.vcjf.weapp.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import cn.vcjf.weapp.api.user.entity.UserProfit;
import cn.vcjf.weapp.api.user.service.UserProfitService;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-25
 */
@Controller
@RequestMapping("/user/userProfit")
public class UserProfitController {
	@Resource
	private UserProfitService userProfitService;

	@RequestMapping("/addUserProfit")
	@ResponseBody
	public TableResult addUserProfit(UserProfit userProfit) {
		userProfit.preInsert();
		boolean b = userProfitService.insert(userProfit);
		return TableResult.Result(b);
	}

	@RequestMapping("/updateUserProfit")
	@ResponseBody
	public TableResult updateUserProfit(UserProfit userProfit) {
		userProfit.preUpdate();
		boolean b = userProfitService.updateById(userProfit);
		return TableResult.Result(b);
	}

	@RequestMapping("/findUserProfitOne")
	@ResponseBody
	public TableResult findUserProfitOne(UserProfit userProfit) {

		EntityWrapper<UserProfit> wrapper = userProfit.getQueryWrapper();

		if (userProfit.getId() != null) {
			wrapper.eq("id", userProfit.getId());
		}
		//Todo

		UserProfit result = userProfitService.selectOne(wrapper);

		return TableResult.OKResult(result);
	}

	/**
	 * 直接接下级
	 * @param userProfit
	 * @return
	 */
	@RequestMapping("/findUserProfitList")
	@ResponseBody
	public TableResult findUserProfitList(UserProfit userProfit) {

		Page<UserProfit> page = userProfit.getPageInfo();

		List<UserProfit> list = userProfitService.selectUserProfitPage(page, userProfit);

		return TableResult.OKResult(page.setRecords(list));

	}

	/**
	 * 间接下级
	 *
	 * @param userProfit
	 * @return
	 */
	@RequestMapping("/selectIndirectSubordinates")
	@ResponseBody
	public TableResult selectIndirectSubordinates(UserProfit userProfit) {
		Page<UserProfit> page = userProfit.getPageInfo();

		List<UserProfit> list = userProfitService.selectIndirectSubordinates(page, userProfit);

		return TableResult.OKResult(page.setRecords(list));

	}

	@RequestMapping("/deleteUserProfit")
	@ResponseBody
	public TableResult deleteUserProfit(UserProfit userProfit) {
		userProfit.preUpdate();
		boolean b = userProfitService.deleteById(userProfit.getId());
		return TableResult.Result(b);
	}

}
